<?
include('header.php');
$id = $db->EscapeString($_GET['id']);

if($_GET['a'] == "skip"){
$sql = $db->Query("SELECT id FROM `youtube` WHERE `id`='".$id."'");
$sit = $db->GetNumRows($sql);
if($sit > 0){
$db->Query("INSERT INTO `viewed` (user_id, site_id) VALUES('".$data['id']."','".$id."')");
}}
?>
<div class="block medium right">
			<div class="top">	
                  <h1>Earn Coins - Youtube</h1>
            </div>
			<div class="content">
<?
  $sql = $db->Query("SELECT id,youtube,title,cpc FROM `youtube` WHERE (`active` = '0' AND `points` >= `cpc`) AND (`id` NOT IN (SELECT `site_id` FROM `viewed` WHERE `user_id`='".$data['id']."') AND `user`!='".$data['id']."') ORDER BY `cpc` DESC LIMIT 0, 1");
  $site = $db->FetchArray($sql);
if($site['id'] > 0){
?>			
<script src="js/swfobject.js"></script>
<script type="text/javascript">
var playing = false;
var fullyPlayed = false;
var interval = '';
var played = 0;
var length = 15;

function YouTubePlaying(){
	played += 0.1;
	roundedPlayed = Math.ceil(played);
	document.getElementById("played").innerHTML = Math.min(roundedPlayed,length);
	if (roundedPlayed == length){
		if (fullyPlayed == false){
			YouTubePlayed();
			fullyPlayed = true;
		}
	}
}
function YouTubePlayed(){
    var response = '<?=$site['youtube']?>';
    $.post("pages/ytreceive.php",
		{  data: response},
		function(result){
			if(!isNaN(result)) { 
				var curr_val = $('#points').text();
				var new_val = parseInt(curr_val)+parseInt(result);
				$('#points').text(new_val); 
			}
		}
	 );
	document.getElementById(response).style.visibility = "visible";
}

function onYouTubePlayerReady(playerId){
	ytplayer = document.getElementById("myytplayer");
	ytplayer.addEventListener("onStateChange", "onYouTubePlayerStateChange");
}
function onYouTubePlayerStateChange(newState){
	if (newState == 1){
		playing = true;
		interval = window.setInterval('YouTubePlaying()',100);
	}else{
		if (playing) window.clearInterval(interval);
		playing = false;
	}
}
  function refreshpage()
   {
      window.location.reload();
   }
</script>
<center>
<div style="width: 520px; padding: 10px;">
<h3>"<?=$site['title']?>"</h3>
View this video for 15 seconds and after that you will receive <b><?=$site['cpc']?> coins</b><br/><br/>
<div id="ytPlayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>
<script type="text/javascript">
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/<?=$site['youtube']?>?enablejsapi=1&playerapiid=ytplayer&autoplay=1", "ytPlayer", "425", "356", "8", null, null, params, atts);
</script>
<br/>

<br />Must play for <span id="played">0</span>/15 seconds (<a href="youtube.php?a=skip&id=<?=$site['id']?>" style="color:blue">Skip</a>)
<div id="<?=$site['youtube']?>" style="visibility:hidden"><a href="javascript:refreshpage()">View Next Video</a></div>
</div></center>
<?}else{?>
<div class="msg">
<div class="error">Sorry, there are no more coins to be earned at the moment. Please try again later.</div>
<div class="info"><a href="buy.php"><b>Feel like you need more coins? You can purchase them now!</b></a></div></div>
<?}?>
				</div>
			</div>		
<?include('footer.php');?>